package jnc;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lib.Couple;
import lib.SortedArray;

import org.apache.commons.lang3.text.WordUtils;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterDescription;

/**
 *  Standalone Java implementation of Neighborhood Correlation
 *	(For python implementation see http://www.neighborhoodcorrelation.org)
 *
 *	(C) 2011 Jacob Joseph <jmjoseph@andrew.cmu.edu>
 *      and Carnegie Mellon University

 *	(C) 2014 Raja Hashim Ali <rhali@kth.se>
 *		and Kungliga Tekniska Hogskolan
	
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 3
 *	of the License, or (at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.

 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

 * @author rhali
 *
 */
public class NC_Standalone {
	HashMap<String, String> blast_info;          // dict of BLAST run parameters
    HashMap<String, Double> nc_info;             // dict of NC run parameters    
	
	public NC_Standalone(double nc_thresh, double smin_factor, String flatfile_name, int num_residues) {
/*		SortedArray sa = new SortedArray(flatfile_name);    
		ArrayList<Double> bitscores = sa.getBitScores();
		ArrayList<Couple<String, String>> blasthits = sa.getBlastHits();
		
		blast_info.put("num_sequences", len(score_dict));
	    if not 'num_residues' in self.blast_info:
	            self.blast_info['num_residues'] = int(537.2148 * len(self.score_dict))
	        blast_info.put("params_expectation", 10 * score_dict.length()));*/
	}

	public static void main(String[] args) {
		Parameters params = new Parameters();
		JCommander jnc = new JCommander(params, args);
		if (args.length == 0 || params.help || params.files == null) {
			StringBuilder sb = new StringBuilder(65536);
			sb.append("Usage: \n\tjava -jar JNC-0.0.1.jar [options] ").append('\n');

			ParameterDescription mainParam = jnc.getMainParameter();
			if (mainParam != null) {
				sb.append("Required arguments\n");
				sb.append("     ").append(mainParam.getDescription()).append('\n');
			}
			List<ParameterDescription> params1 = jnc.getParameters();
			Field[] fields = params.getClass().getFields();
			for (Field f : fields) {
				for (ParameterDescription p : params1) {
					if (f.getName().equals(p.getField().getName())) {
						sb.append(p.getNames()).append('\n');
						String def = (p.getDefault() == null ? "" : " Default: " + p.getDefault().toString() + '.');
						String desc = WordUtils.wrap(p.getDescription() + def, 120);
						desc = "     " + desc;
						desc = desc.replaceAll("\n", "\n     ") + '\n';
						sb.append(desc);
						break;
					}
				}
			}
			System.out.println(sb.toString());
			return;
		}

	    double nc_thresh = Double.parseDouble(params.nc_thresh);
	    double smin_factor = Double.parseDouble(params.smin_factor);
	    String flatfile_name = params.files.get(0);
	    String output_name = params.output_name;
	    int num_residues = Integer.parseInt(params.num_residues);
	        
/*	    ncrun = nc_standalone(nc_thresh, smin_factor,
	                         flatfile_name, num_residues)

	    if (output_name != null)
	        fd_out = open(output_name, 'w');
	    else
	        fd_out = sys.stdout;

	    ncrun.calculate_nc( ncrun.score_dict, fd_out = fd_out,seq_id_map = ncrun.seq_id_map);

	    if (fd_out != sys.stdout)
	    	fd_out.close();
*/	}

}
